<?php
/**
 * @des下载分类管理 
 * @date 2019-07-13
 * @author ljm
 */

namespace App\Http\Controllers\Admin;

use App\Models\DownloadCat;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class DownloadCatController extends Controller
{
    /**
     * @var
     */
    public $downloadCatModel;

    /**
     * DownloadCatController constructor.
     */
    public function __construct()
    {
        $this->downloadCatModel = new DownloadCat();
    }

    /**
     *下载分类列表展示
     */
    public function index(){
        return view('admin.downloadcat.index');
    }

    /**
     *
     */
    public function getList(Request $request){
        $params=[
            'where'=>[['is_delete',1]],
            'getfields'=>false,
            'field'=>['id','pid','title','description','status','psort','created_at','updated_at'],
            'order'=>'created_at'
        ];
        $res = $this->downloadCatModel->getList($params);
        $data = [
            'code' => 0,
            'msg' => '正在请求中...',
            'data' => $res['row']
        ];
        return response()->json($data);


    }

    /**
     *
     */
    public function add(){
       // print_r($this->treeList());
        return view('admin.downloadcat.add',['treeList'=>$this->treeList()]);
    }

    /**
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function addSave(Request $request){
        $params = $request->input();
        $res = $this->downloadCatModel->saveAdd($params);
        if($res){
            return response()->json(array('code'=>200,'msg'=>''));
        }else{
            return response()->json(array('code'=>310,'msg'=>''));
        }

    }
    public function del(){

    }

    /**
     *
     */
    private function treeList(){
        $params=[
            'where'=>[['is_delete',1], ['status',1]],
            'field'=>['id','pid','title'],
            'getfields'=>false,
            'order'=>'created_at'
        ];
        $res = $this->downloadCatModel->getList($params);
        return treeNode(objectToArray($res['row']));

    }
}
